import { defineStore } from 'pinia';
import * as settlementApi from '@/api/settlement';
import { Message } from '@arco-design/web-vue';

const useSettlementStore = defineStore('settlement', () => {

    async function getSettlement(param?: any) {
        const response: any = await settlementApi.querySettlementDetail(param)
        if (response.code === 20000) {
            return response.data.settlement
        }
        return {}
    }

    async function getSettlements(param?: any) {
        const response: any = await settlementApi.querySettlementList(param)
        if (response.code === 20000) {
            return response.data.settlements
        }
        return []
    }

    async function deleteSettlement(param: { id?: number }) {
        const response: any = await settlementApi.deleteSettlement(param)
        if (response.code === 20000) {
            Message.success('移除成功');
        }
    }

    async function saveSettlementCustomers(dataList: any) {
        await settlementApi.updateSettlementCustomers(dataList);
        Message.success('保存成功');
    }

    async function saveSettlement(param: any) {
        if (param.id) {
            await settlementApi.updateSettlement(param);
        } else {
            await settlementApi.createSettlement(param);
        }
        if (param.customers) {
            await settlementApi.updateSettlementCustomers(param.customers);
        }
        Message.success('保存成功');
    }

    async function getSettlementCustomers(param?: any) {
        const response: any = await settlementApi.querySettlementCustomerList(param)
        if (response.code === 20000) {
            return response.data.settlementCustomers
        }
        return []
    }

    async function deleteSettlementCustomer(param: { id?: number }) {
        const response: any = await settlementApi.deleteSettlementCustomer(param)
        if (response.code === 20000) {
            Message.success('移除成功');
        }
    }

    async function saveSettlementCustomer(param: any) {
        if (param.id) {
            await settlementApi.updateSettlementCustomer(param);
        } else {
            await settlementApi.createSettlementCustomer(param);
        }
        Message.success('保存成功');
    }

    return { saveSettlementCustomers, getSettlement, getSettlements, deleteSettlement, saveSettlement, getSettlementCustomers, deleteSettlementCustomer, saveSettlementCustomer }
});

export default useSettlementStore;
